package com.finger.forest.adapter;

import android.graphics.drawable.Drawable;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.finger.forest.R;
import com.finger.forest.model.BaseCommonResourceModel;
import com.finger.forest.model.ResourceTypeModel;

import java.util.List;

/**
 * @author sy
 * @date 2021/1/30
 * @desc
 */
public class ResourceTypeAdapter extends BaseMultiItemQuickAdapter<BaseCommonResourceModel, BaseViewHolder> {

    public int mPosition = -1;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public ResourceTypeAdapter(List<BaseCommonResourceModel> data) {
        super(data);

        addItemType(BaseCommonResourceModel.TYPE_PRENT, R.layout.item_group_name);
        addItemType(BaseCommonResourceModel.TYPE_CHILD, R.layout.item_child_name);
    }

    @Override
    protected void convert(BaseViewHolder helper, BaseCommonResourceModel item) {
        int type = item.getItemType();
        switch (type) {
            case BaseCommonResourceModel.TYPE_PRENT:
                ResourceTypeModel resourceTypeModel = item.getResourceTypeModel();
                if (null != resourceTypeModel) {
                    helper.setText(R.id.group_name, resourceTypeModel.getRtName());
                }
                break;

            case BaseCommonResourceModel.TYPE_CHILD:
                ResourceTypeModel.ChildrenBean childrenBean = item.getChildrenBean();
                if (null != childrenBean) {
                    TextView child_name = helper.getView(R.id.child_name);
                    child_name.setText(childrenBean.getRtName());

                    helper.addOnClickListener(R.id.child_name);

                    Drawable drawable = null;
                    if (mPosition == helper.getAdapterPosition()) {
                        drawable = ContextCompat.getDrawable(mContext, R.mipmap.radio_select);
                    } else {
                        drawable = ContextCompat.getDrawable(mContext, R.mipmap.radio_unselect);
                    }
                    child_name.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
                }
                break;
        }
    }
}
